#! /bin/bash

# ------------------------------------------
# Created by Statemood, 2015.06.09 14:50
# Updated by Statemood, 2015.06.10 18:00
#            Statemood@gmail.com
#
# Project monitor:
#       https://github.com/Statemood/monitor
# ------------------------------------------

 mysql='/usr/local/mysql/bin/mysql'
  host=''
  port='3306'
  user='cacti'
  pass='KoBEK2sBsxc'
  temp=`mktemp /dev/shm/XXXXXXXXXXXXXXXX`

DISP_COUNTS=0
DISP_DETAIL=1

Usage(){
    help_msg="
    \033[1m$(basename $0)\033[0m
        Get MySQL Status & Processlist info for Cacti.

            -H, --host      Hostname or IP
            -P, --port      MySQL port, default 3306
            -u, --user      MySQL user
            -p, --password  Password of the MySQL user
            -d, --details   Display short names
            -i, --item      Item data:
                                IA, InnoDB_Activity
                                IR, InnoDB_Row
                                IP, InnoDB_Pending
                                ML, MySQL_Log
                                CM, Command
                                CN, Connections_Network
                                FT, Files_Tables
                                HD, Handlers
                                QC, Query_Cache
                                ST, Select_Types
                                SR, Sorts
                                TL, Table_Locks
                                SQ, Slow_Query
    "
    echo -e "$help_msg"
}

for args in "$@"
do
    case "$args" in
        -H|--host)
            host="$2"
            shift 2
            ;;
        -P|--port)
            port="$2"
            shift 2
            ;;
        -u|--user)
            user="$2"
            shift 2
            ;;
        -p|--password)
            pass="$2"
            shift 2
            ;;
        -i|--item)
            item="$2"
            shift
            ;;
        -d|--details)
            DISP_DETAIL=0
            ;;
        -h|--help)
            Usage
            exit 0
            ;;
        -*)
            Usage
            exit 1
            ;;
    esac
done

Clean(){
    test -f "$temp" && rm -rf "$temp"
}

DBQuery(){
    $mysql -h $host -u $user -p$pass -e "$1" | sed 's/\t/ /g' > $temp
}

Print_Results(){
    for str in $Strings
    do
        if [ $DISP_COUNTS -le 9 ]
        then
            NUM=0$DISP_COUNTS
        else
            NUM=$DISP_COUNTS
        fi

        if [ $DISP_DETAIL = 0 ]
        then
            echo -e "${item}$NUM\t--> $str"
        else
            echo ${item}$NUM:`grep ^$str $temp | awk '{print $2}'`
        fi
        echo $((DISP_COUNTS++)) > /dev/null
    done 
}

if [ "$item" = "PL" ]
then
    DBQuery "show full processlist"
else
    DBQuery "show status"
fi

case "$item" in
    CM|Command)
        Strings="Com_commit Com_flush   Questions   Queries     Com_select
                 Com_delete Com_insert  Com_update  Com_replace Com_load 
                 Com_delete_multi       Com_insert_select
                 Com_update_multi       Com_replace_select" ;;

    CN|Connections_Network)
        Strings="Max_used_connections   Aborted_clients     Aborted_connects
                 Threads_connected  Threads_running  Threads_created Threads_cached
                 Connections  Bytes_sent  Bytes_received" ;;

    FT|Files_Tables)
        Strings="Open_tables   Open_files      Opened_files     Opened_tables
                 Created_tmp_disk_tables  Created_tmp_files     Created_tmp_tables" ;;
        
    HD|Handlers)
        Strings="Handler_write  Handler_update  Handler_commit  Handler_delete
                 Handler_external_lock  Handler_read_first      Handler_read_key
                 Handler_read_next  Handler_read_prev           Handler_read_rnd 
                 Handler_read_rnd_next" ;;

    IA|InnoDB_Activity)
        Strings="Innodb_page_size   Innodb_pages_created    Innodb_buffer_pool_pages_data
                 Innodb_pages_read  Innodb_pages_written    Innodb_buffer_pool_pages_flushed 
                 Innodb_buffer_pool_bytes_data              Innodb_buffer_pool_pages_free
                 Innodb_buffer_pool_pages_total             Innodb_buffer_pool_wait_free";;
        
    IP|InnoDB_Pending)
        Strings="Innodb_data_pending_fsyncs  Innodb_data_pending_reads  Innodb_data_pending_writes" ;;

    IR|InnoDB_Row)
        Strings="Innodb_rows_deleted    Innodb_rows_inserted    Innodb_rows_read
                 Innodb_rows_updated    Innodb_row_lock_current_waits   Innodb_row_lock_waits
                 Innodb_row_lock_time   Innodb_row_lock_time_avg        Innodb_row_lock_time_max" ;;
        
    ML|MySQL_Log)
        Strings="Innodb_log_writes  Innodb_log_waits    Innodb_log_write_request
                 Binlog_cache_use   Binlog_cache_disk_use"  ;;

    PL|Processlist)
        echo 123
        ;;
    QC|Query_Cache)
        Strings="Qcache_queries_in_cache  Qcache_total_blocks Qcache_hits    Qcache_not_cached
                 Qcache_lowmem_prunes     Qcache_free_blocks Qcache_free_memory  Qcache_inserts" ;;
        
    ST|Select_Types)
        Strings="Select_full_join Select_full_range_join Select_range Select_range_check Select_scan" ;;
        
    SQ|Slow_Query)
        Strings="Slow_queries   Slow_launch_threads"    ;;
        
    SR|Sorts)
        Strings="Sort_rows      Sort_range      Sort_merge_passes   Sort_scan"  ;;
        
    TL|Table_Locks)
        Strings="Table_locks_immediate          Table_locks_waited" ;;
        
esac

if [ $DISP_DETAIL = 0 ]
then
    Print_Results
else
    Print_Results  | tr -s "\n" " "
fi    
Clean

